import { defineStore } from "pinia";
import { Hospital, HospitalDepartment, Result } from "@/types/types.ts";
import { doGetHospitalDepartments, doGetHospitalDetail } from "@/api/hosiptal";
import { ElMessage } from "element-plus";

const useHospitalDetailStore = defineStore("hospitalDetail", {
  state: (): {
    hospitalDetail: Hospital;
    hospitalDepartments: HospitalDepartment[];
  } => {
    return {
      hospitalDetail: {} as Hospital,
      hospitalDepartments: [] as HospitalDepartment[],
    };
  },
  actions: {
    async getHospitalInfo(hospcode: string) {
      if (!hospcode) {
        ElMessage.error("请输入医院代码");
        return;
      }
      let result: Result | void = await doGetHospitalDetail(<string>hospcode);
      if (result?.code === 200) {
        this.hospitalDetail = result.data as Hospital;
      } else {
        ElMessage.error(result.msg);
      }
    },
    async getHospitalDepartments(hoscode: string) {
      let result: Result | void = await doGetHospitalDepartments(hoscode);
      if (result?.code === 200) {
        this.hospitalDepartments = result.data as HospitalDepartment[];
      }
      console.log(result);
    },
  },
  getters: {},
});
export default useHospitalDetailStore;
